package com.winit.openapi.claim.builder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.model.ResponseMsg;

@Service("claimOrderBuilder")
public class ClaimOrderBuilder {

    public Map<String, Object> buildClaimOrderRequest(JSONObject input) {
        Map<String, Object> data = new HashMap<String, Object>();
        if (input.containsKey("orderId")) {
            data.put("base_id", input.get("orderId"));

        }
        if (input.containsKey("trackingNo")) {
            data.put("awb_no", input.get("trackingNo"));

        }
        //添加单号入参
        if (input.containsKey("orderNo")) {
            data.put("winit_order_no", input.get("orderNo"));

        }
        if (input.containsKey("type")) {
            data.put("claim_type", input.get("type"));

        }
        if (input.containsKey("quantity")) {
            data.put("quantity_goods", input.get("quantity"));

        }
        if (input.containsKey("totalAmount")) {
            data.put("gross_of_goods", input.get("totalAmount"));

        }
        if (input.containsKey("lastMileAmount")) {
            data.put("end_of_freight", input.get("lastMileAmount"));

        }
        if (input.containsKey("rateOfCNYToLocal")) {
            data.put("local_rate", input.get("rateOfCNYToLocal"));

        }
        if (input.containsKey("describeEN")) {
            data.put("describe_en", input.get("describeEN"));

        }
        if (input.containsKey("reason")) {
            data.put("reason", input.get("reason"));

        }
        if (input.containsKey("valueOfProofURL")) {
            data.put("value_of_proof", input.get("valueOfProofURL"));

        }
        if (input.containsKey("cargoPhotosURL")) {
            data.put("cargo_photos", input.get("cargoPhotosURL"));

        }
        if (input.containsKey("status")) {
            data.put("status", input.get("status"));

        }
        if (input.containsKey("source")) {
            data.put("source", input.get("source"));

        }
        if (input.containsKey("ctx")) {
            data.put("ctx", input.get("ctx"));

        }

        return data;

    }

    public Map<String, Object> buildClaimOrderEvaluate(JSONObject input) {
        Map<String, Object> data = new HashMap<String, Object>();
        if (input.containsKey("orderId")) {
            data.put("base_id", input.get("orderId"));

        }
        if (input.containsKey("evaluateLevel")) {
            data.put("evaluate_level", input.get("evaluateLevel"));

        }
        if (input.containsKey("evaluateDetails")) {
            data.put("evaluate_details", input.get("evaluateDetails"));

        }
        if (input.containsKey("ctx")) {
            data.put("ctx", input.get("ctx"));

        }
        return data;
    }

    public Object buildAcceptInfoResponse(ResponseMsg responseMsg) {
        Object dataObject = responseMsg.getData();
        if (null == dataObject || StringUtils.isEmpty(dataObject.toString())) {
            return dataObject;
        }
        JSONObject responseData = JSONObject.parseObject(responseMsg.getData().toString());
        Map<String, Object> data = new HashMap<String, Object>();
        if (responseData.containsKey("base_id")) {
            data.put("orderId", responseData.get("base_id"));

        }
        if (responseData.containsKey("accept_time")) {
            data.put("acceptTime", responseData.get("accept_time"));

        } else {
            data.put("acceptTime", null);
        }
        if (responseData.containsKey("accept_service")) {
            data.put("customerService", responseData.get("accept_service"));

        } else {
            data.put("customerService", null);
        }
        if (responseData.containsKey("result")) {
            data.put("result", responseData.get("result"));

        } else {
            data.put("result", null);
        }
        if (responseData.containsKey("refuse_reason")) {
            data.put("refuseReason", responseData.get("refuse_reason"));

        } else {
            data.put("refuseReason", null);
        }
        if (responseData.containsKey("arrival_date")) {
            data.put("arrivalDate", responseData.get("arrival_date"));

        } else {
            data.put("arrivalDate", null);
        }
        if (responseData.containsKey("end_date")) {
            data.put("endDate", responseData.get("end_date"));

        } else {
            data.put("endDate", null);
        }
        if (responseData.containsKey("payment_currency")) {
            data.put("paymentCurrencyCode", responseData.get("payment_currency"));

        } else {
            data.put("paymentCurrencyCode", null);
        }
        if (responseData.containsKey("payment_money")) {
            data.put("paymentAmount", responseData.get("payment_money"));

        } else {
            data.put("paymentAmount", null);
        }
        if (responseData.containsKey("service_feedback")) {
            data.put("serviceFeedback", responseData.get("service_feedback"));

        } else {
            data.put("serviceFeedback", null);
        }
        if (responseData.containsKey("annex")) {
            data.put("annex", responseData.get("annex"));

        } else {
            data.put("annex", null);
        }
        if (responseData.containsKey("annex_feedback")) {
            data.put("annexFeedback", responseData.get("annex_feedback"));

        } else {
            data.put("annexFeedback", null);
        }
        if (responseData.containsKey("msg_feedback")) {
            data.put("msgFeedback", responseData.get("msg_feedback"));

        } else {
            data.put("msgFeedback", null);
        }
        if (responseData.containsKey("supplier_paytime")) {
            data.put("supplierConfirmPayTime", responseData.get("supplier_paytime"));

        } else {
            data.put("supplierConfirmPayTime", null);
        }
        if (responseData.containsKey("supplier_arrivaltime")) {
            data.put("supplierConfirmArrivalTime", responseData.get("supplier_arrivaltime"));

        } else {
            data.put("supplierConfirmArrivalTime", null);
        }

        if (responseData.containsKey("complete_time")) {
            data.put("completeTime", responseData.get("complete_time"));

        } else {
            data.put("completeTime", null);
        }

        return data;
    }

    public Object buildOrderDetailResponse(ResponseMsg responseMsg) {
        Object dataObject = responseMsg.getData();
        if (null == dataObject || StringUtils.isEmpty(dataObject.toString())) {
            return dataObject;
        }
        JSONObject responseData = JSONObject.parseObject(responseMsg.getData().toString());
        Map<String, Object> data = new HashMap<String, Object>();

        if (responseData.containsKey("base")) {
            data.put("base", buildBaseData(JSONObject.parseObject(responseData.get("base").toString())));
        }
        if (responseData.containsKey("info")) {
            data.put("info", buildInfoData(JSONObject.parseObject(responseData.get("info").toString())));
        }

        return data;
    }

    private Map<String, Object> buildBaseData(JSONObject input) {
        Map<String, Object> data = new HashMap<String, Object>();
        if (input.containsKey("base_id")) {
            data.put("orderId", input.get("base_id"));

        }
        if (input.containsKey("serial_no")) {
            data.put("serialNo", input.get("serial_no"));

        } else {
            data.put("serialNo", null);
        }
        if (input.containsKey("customer_code")) {
            data.put("customerCode", input.get("customer_code"));

        } else {
            data.put("customerCode", null);
        }
        if (input.containsKey("customer_name")) {
            data.put("customerName", input.get("customer_name"));

        } else {
            data.put("customerName", null);
        }
        if (input.containsKey("type")) {
            data.put("type", input.get("type"));

        } else {
            data.put("type", null);
        }
        if (input.containsKey("action_datetime")) {
            data.put("acceptTime", input.get("action_datetime"));

        } else {
            data.put("acceptTime", null);
        }
        if (input.containsKey("action_user")) {
            data.put("accepter", input.get("action_user"));

        } else {
            data.put("accepter", null);
        }
        if (input.containsKey("end_datetime")) {
            data.put("acceptEndTime", input.get("end_datetime"));

        } else {
            data.put("acceptEndTime", null);
        }
        if (input.containsKey("end_user")) {
            data.put("acceptEndUser", input.get("end_user"));

        } else {
            data.put("acceptEndUser", null);
        }
        if (input.containsKey("status")) {
            data.put("status", input.get("status"));

        } else {
            data.put("status", null);
        }
        if (input.containsKey("evaluate_level")) {
            data.put("evaluateLevel", input.get("evaluate_level"));

        } else {
            data.put("evaluateLevel", null);
        }
        if (input.containsKey("evaluate_details")) {
            data.put("evaluateDetails", input.get("evaluate_details"));

        } else {
            data.put("evaluateDetails", null);
        }
        if (input.containsKey("source")) {
            data.put("source", input.get("source"));

        } else {
            data.put("source", null);
        }
        if (input.containsKey("apply_time")) {
            data.put("applyTime", input.get("apply_time"));

        } else {
            data.put("applyTime", null);
        }

        return data;
    }

    private Map<String, Object> buildInfoData(JSONObject input) {
        Map<String, Object> data = new HashMap<String, Object>();
        if (input.containsKey("base_id")) {
            data.put("orderId", input.get("base_id"));

        }
        if (input.containsKey("awb_no")) {
            data.put("trackingNo", input.get("awb_no"));

        } else {
            data.put("trackingNo", null);
        }
        if (input.containsKey("claim_type")) {
            data.put("type", input.get("claim_type"));

        } else {
            data.put("type", null);
        }
        if (input.containsKey("winit_order_no")) {
            data.put("winitOrderNo", input.get("winit_order_no"));

        } else {
            data.put("winitOrderNo", null);
        }
        if (input.containsKey("delivery_time")) {
            data.put("outboundTime", input.get("delivery_time"));

        } else {
            data.put("outboundTime", null);
        }
        if (input.containsKey("outbound_status")) {
            data.put("outboundStatus", input.get("outbound_status"));

        } else {
            data.put("outboundStatus", null);
        }
        if (input.containsKey("warehouse")) {
            data.put("warehouseName", input.get("warehouse"));

        } else {
            data.put("warehouseName", null);
        }
        if (input.containsKey("taking_delivery")) {
            data.put("carrier", input.get("taking_delivery"));

        } else {
            data.put("carrier", null);
        }
        if (input.containsKey("delivery_mode")) {
            data.put("deliveryWayName", input.get("delivery_mode"));

        } else {
            data.put("deliveryWayName", null);
        }
        if (input.containsKey("good_num")) {
            data.put("outboundQuantity", input.get("good_num"));

        } else {
            data.put("outboundQuantity", null);
        }
        if (input.containsKey("foot_freight")) {
            data.put("lastMileAmount", input.get("foot_freight"));

        } else {
            data.put("lastMileAmount", null);
        }
        if (input.containsKey("quantity_goods")) {
            data.put("claimQuantity", input.get("quantity_goods"));

        } else {
            data.put("claimQuantity", null);
        }
        if (input.containsKey("currency")) {
            data.put("claimCurrency", input.get("currency"));

        } else {
            data.put("claimCurrency", null);
        }
        if (input.containsKey("end_of_freight")) {
            data.put("claimLastMileAmount", input.get("end_of_freight"));

        } else {
            data.put("claimLastMileAmount", null);
        }
        if (input.containsKey("gross_of_goods")) {
            data.put("claimTotalAmount", input.get("gross_of_goods"));

        } else {
            data.put("claimTotalAmount", null);
        }
        if (input.containsKey("reason")) {
            data.put("reason", input.get("reason"));

        } else {
            data.put("reason", null);
        }
        if (input.containsKey("describe_en")) {
            data.put("describeEN", input.get("describe_en"));

        } else {
            data.put("describeEN", null);
        }

        return data;
    }

    public Map<String, Object> buildQueryOrderListRequest(JSONObject input) {
        Map<String, Object> data = new HashMap<String, Object>();
        if (input.containsKey("customerCode")) {
            data.put("customer_code", input.get("customerCode"));
        }
        if (input.containsKey("status")) {
            data.put("status", input.get("status"));
        }
        if (input.containsKey("pageParams")) {
            data.put("pageVo", input.get("pageParams"));
        } else {
            // 没有传分页参数的，给出默认值
            Map<String, Object> page = new HashMap<String, Object>();
            page.put("page", ApiConstant.PAGE_NO_DEFAULT);
            page.put("pageSize", ApiConstant.PAGE_SIZE_DEFAULT);
            data.put("pageVo", page);
        }
        if (input.containsKey("ctx")) {
            data.put("ctx", input.get("ctx"));

        }

        return data;
    }

    public Object buildOrderListResponse(ResponseMsg responseMsg) {
        Object dataObject = responseMsg.getData();
        if (null == dataObject || StringUtils.isEmpty(dataObject.toString())) {
            return dataObject;
        }
        Map<String, Object> data = new HashMap<String, Object>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        JSONObject responseData = JSONObject.parseObject(responseMsg.getData().toString());
        if (responseData.containsKey("content")) {
            JSONArray content = responseData.getJSONArray("content");
            for (Object object : content) {
                Map<String, Object> orderData = new HashMap<String, Object>();
                JSONObject order = (JSONObject) object;
                orderData.putAll(buildBaseData(order));
                orderData.putAll(buildInfoData(order));
                list.add(orderData);
            }
        }
        // 构建分页信息
        Map<String, Object> pageData = new HashMap<String, Object>();
        if (responseData.containsKey("pageable")) {
            JSONObject pageVo = (JSONObject) responseData.get("pageable");
            pageData.put("currentPageNum", pageVo.get("page"));
            pageData.put("currentPageSize", pageVo.get("size"));
        }

        if (responseData.containsKey("total")) {
            pageData.put("total", responseData.get("total"));
        }

        data.put("list", list);
        data.put("pageable", pageData);

        return data;
    }
}
